package com.shenfa.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.shenfa.bean.City;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.bean.TreeNode;
import com.shenfa.bean.TreeNodeCheck;
/**
 * 
 * @author yanglong
 * 
 * @date 2011-8-19
 *
 */
@SuppressWarnings({ "unchecked", "finally" })
public class CityDao {

	@Autowired
	private SqlMapClient sqlMapClient;
	
	/**
	 * 查询所有城市列表
	 */
	public List<City> queryAllList(){
		List<City> list = null;
		try {
			list = sqlMapClient.queryForList("city_manager.getAllList");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			return list;
		}
	}

	/**
	 * 根据城市编号查询所有子城市列表
	 * @param city	城市对象
	 * @return
	 */
	public List<City> queryListByParentno(City city) {
		List<City> list = null;
		try {
			list = sqlMapClient.queryForList("city_manager.getListByParentno",city);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			return list;
		}
	}

	/**
	 * 添加城市
	 * @param city
	 */
	public int insertCity(City city) {
		try {
			sqlMapClient.insert("city_manager.add", city);
			return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	/**
	 * 更新城市信息
	 * @param city
	 */
	public int updateCity(City city) {
		try {
			sqlMapClient.update("city_manager.upd", city);
			return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 删除城市信息
	 * @param city
	 */
	public int deleteCity(City city) {
		try {
			sqlMapClient.delete("city_manager.del", city);
			return 0;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	/**
	 * 查询城市
	 * @param node
	 * @return
	 */
	public List<TreeNode> queryTree(String node) {
		List<TreeNode> list = null;
		try {
			list = sqlMapClient.queryForList("city_manager.getTree",node);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<TreeNodeCheck> queryCheckTree(String node) {
		List<TreeNodeCheck> list = null;
		try {
			list = sqlMapClient.queryForList("city_manager.getCheckTree",node);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	/**
	 * 根据对象条件查询对象集合
	 * @param city	查询对象
	 * @return
	 */
	public List<City> queryList(City city, int start, int limit) {
		List<City> list = null;
		try {
			list = sqlMapClient.queryForList("city_manager.getList", city, start, limit);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			return list;
		}
	}

	/**
	 * 查询总记录条数
	 * @param city	查询条件
	 * @return	总记录条数
	 */
	public int queryCount(City city) {
		int a;
		try {
			a =(Integer) sqlMapClient.queryForObject("city_manager.getListCount", city);
			
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
           
		return a;
	}

}
